<template>
	<div class="history-project custom-scrollbar">
		<template v-if="!notProject">
			<project-admin-list :projectList="historyProjects" :curProjectIndex="curProjectIndex" @handleSelectProject="handleSelectProject">
			</project-admin-list>
			<view class="project-content">
				<view class="p_c-header">
					<project-admin-navbar type="history" :projectList="historyProjects" :relevanceUserID="relevanceUserID" :curProjectIndex="curProjectIndex"
					 @handleProject="handleDeleteProject">
					 </project-admin-navbar>
				</view>
				<view class="p_c-body">
					<template v-if="historyProjects.length > 0">
						<project-admin-body type="history" :projectList="historyProjects" :relevanceUserID="relevanceUserID" :curProjectIndex="curProjectIndex"
						 @showMyProjectModule="showMyProjectModule" @handleDownLoadModule="handleDownLoadModule">
						 </project-admin-body>
					</template>
				</view>
			</view>

			<!-- 查看队友负责的项目模块 -->
			<el-drawer :title="curUserData.username" :visible.sync="drawer" direction="ltr">
				<el-tree :data="curUserData.burdenModule" default-expand-all></el-tree>
			</el-drawer>

		</template>
		<template v-if="notProject">
			<el-alert :closable="false" title="提示" type="warning" description="您没有正在进行的项目,请先去编辑项目,项目管理-->编辑项目" show-icon>
			</el-alert>
		</template>
	</div>
</template>

<script>
	import {mapState,mapMutations} from "vuex";
	import alioss from "@/common/aliyun.oss.client.js";
	export default {
		data() {
			return {
				notProject: false,
				historyProjects: [],
				curProjectIndex: 0,
				curUserData:{},
				drawer: false
			}
		},
		computed: {
			...mapState(['relevanceUserID'])
		},
		watch: {
			relevanceUserID(newVal, oldVal) {
				this.getCurPageData()
			}
		},
		created() {
			if (this.relevanceUserID !== -1) this.getCurPageData();
		},
		methods: {
			async getCurPageData() {
				let {
					relevanceUserID
				} = this;
				this.notProject = false;
				let res = await this.$network({
					api: "getHistoryProject",
					data: {
						relevanceUserID
					}
				})

				if (res.status === 0) {
					this.historyProjects = res.data;
				}
				if(res.status === 1){
					this.notProject = true;
				}
			},
			handleSelectProject(item, index) {
				this.curProjectIndex = index;
			},
			handleDeleteProject() {
				this.$alert('确认永久删除该项目？', '提示', {
					confirmButtonText: '取消',
					showCancelButton: true,
					cancelButtonText: "确定",
					showClose:false,
					type: "warning",
					callback: action => {
						if (action === "cancel" || action === "close") {
							this.$message({
								type: 'warning',
								message: `action: ${ action }`
							});
						}
					}
				});
			},
			showMyProjectModule(item, index) {
				this.drawer = true;
				this.curUserData = item;
			},
			handleDownLoadModule(item,index){
				if(item.finishProjectDoc.fileName === ""){
					this.$message.warning("该队友还没有上传项目模块")
					return false;
				}
				 let downloadUrl = alioss.getDownloadUrl(item.finishProjectDoc.fileName);
				 window.open(downloadUrl);
			}
		}
	}
</script>

<style lang="scss">
	.history-project {
		height: 100%;
		.project-content {
			margin-top: 20px;

			.p_c-header {
				@include f-ai-jc(flex, center, flex-end)
			}
		}
	}
</style>
